package com.gkj.blog.util;


import com.gkj.blog.business.consts.SessionConst;
import com.gkj.blog.business.vo.entity.User;
import com.gkj.blog.framework.holder.RequestHolder;
import com.google.code.kaptcha.Constants;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

public class SessionUtil {

    public static List<String> getEditImages(){
        return (List<String>) RequestHolder.getSession().getAttribute(SessionConst.EDIT_IMAGES);
    }

    public static void removeSubmitEditImages(String[] submitList){
        List<String> editImages= (List<String>) RequestHolder.getSession().getAttribute(SessionConst.EDIT_IMAGES);
        if(editImages==null)return;
        for(String submitImage:submitList){
            for(String e:editImages){
                if(e.indexOf(submitImage)!=-1){
                    editImages.remove(e);
                }
            }
        }
    }

    public static void addEditImage(String realPath){
        List<String> editImages= (List<String>) RequestHolder.getSession().getAttribute(SessionConst.EDIT_IMAGES);
        if(editImages==null){
            editImages=new LinkedList<String>();
        }
        editImages.add(realPath);
        RequestHolder.getSession().setAttribute(SessionConst.EDIT_IMAGES,editImages);
    }
    /**
     * 获取session中的用户信息
     *
     * @return User
     */
    public static User getUser() {
        return (User) RequestHolder.getSession(SessionConst.USER_SESSION_KEY);
    }

    /**
     * 添加session
     *
     * @param user
     */
    public static void setUser(User user) {
        RequestHolder.setSession(SessionConst.USER_SESSION_KEY, user);
    }

    /**
     * 删除session信息
     */
    public static void removeUser() {
        RequestHolder.removeSession(SessionConst.USER_SESSION_KEY);
    }

    /**
     * 获取session中的Token信息
     *
     * @return String
     */
    public static String getToken(String key) {
        return (String) RequestHolder.getSession(key);
    }

    /**
     * 添加Token
     */
    public static void setToken(String key) {
        RequestHolder.setSession(key, UUID.randomUUID().toString());
    }

    /**
     * 删除Token信息
     */
    public static void removeToken(String key) {
        RequestHolder.removeSession(key);
    }

    /**
     * 获取验证码
     */
    public static String getKaptcha() {
        return (String) RequestHolder.getSession(Constants.KAPTCHA_SESSION_KEY);
    }

    /**
     * 保存验证码
     */
    public static void setKaptcha(String kaptcha) {
        RequestHolder.setSession(Constants.KAPTCHA_SESSION_KEY, kaptcha);
    }

    /**
     * 保存验证码
     */
    public static void removeKaptcha() {
        RequestHolder.removeSession(Constants.KAPTCHA_SESSION_KEY);
    }

    /**
     * 删除所有的session信息
     */
    public static void removeAllSession() {
        String[] keys = RequestHolder.getSessionKeys();
        if (keys != null && keys.length > 0) {
            for (String key : keys) {
                RequestHolder.removeSession(key);
            }
        }
    }
}
